package com.train.common.utils.date;

import cn.hutool.core.util.StrUtil;
import io.micrometer.common.util.StringUtils;
import io.netty.util.internal.StringUtil;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class LocalDateTimeUtils {

    public static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

    //时间格式"HH:mm:ss"
    public static final String TIME_FORMAT = "HH:mm:ss";

    //时间格式"yyyy-MM-dd"
    public static final String DATE_FORMAT = "yyyy-MM-dd";

    /**
     * LocalDateTime转Date
     * @param localDateTime
     * @return
     */
    public static Date toDate(LocalDateTime localDateTime) {
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    /**
     * 根据格式获取当前时间
     * @param format
     * @return
     */
    public static LocalDateTime getNow(String format) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
        return LocalDateTime.parse(getNowStr(format), formatter);
    }

    /**
     * 根据格式获取当前时间字符串
     * @param format
     * @return
     */
    public static String getNowStr(String format) {
        if (StrUtil.isBlank(format)) {
            format = DATE_TIME_FORMAT;
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
        return LocalDateTimeUtils.getNow().format(formatter);
    }

    /**
     * 获取当前时间
     * @return
     */
    public static LocalDateTime getNow() {
        return LocalDateTime.now();
    }

    /**
     * 根据格式解析时间字符串
     * @param text
     * @param dateTimeFormat
     * @return
     */
    public static LocalDateTime parse(String text, String dateTimeFormat) {
        if (StringUtil.isNullOrEmpty(text)) {
            return null;
        }
        if (StringUtil.isNullOrEmpty(dateTimeFormat)) {
            dateTimeFormat = DATE_TIME_FORMAT;
        }
        //转换时间字符串为localdatetime,时区设置东8区
        LocalDateTime localDateTime = LocalDateTime.parse(text, DateTimeFormatter.ofPattern(dateTimeFormat));
        return localDateTime.atZone(ZoneId.of("Asia/Shanghai")).toLocalDateTime();
    }

    /**
     * date转localdatetime
     * @param date
     */
    public static LocalDateTime parse(Date date) {
        if (date == null) {
            return null;
        }
        return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
    }

    /**
     * LocalDateTime格式化时间字符串
     * @param dateTime
     * @param dateFormat
     * @return
     */
    public static String format(LocalDateTime dateTime, String dateFormat) {
        if (dateTime == null) {
            return "";
        }
        return dateTime.format(DateTimeFormatter.ofPattern(dateFormat));
    }

    /**
     * LocalDate格式化时间字符串
     * @param date
     * @param dateFormat
     * @return
     */
    public static String format(LocalDate date, String dateFormat) {
        if (date == null) {
            return "";
        }
        return date.format(DateTimeFormatter.ofPattern(dateFormat));
    }

    public static void main(String[] args) {
        System.out.println(LocalDateTimeUtils.parse("2025-02-15 23:50:27", DATE_TIME_FORMAT));
    }

}
